#pragma once

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <mutex>
#include <unordered_map>
#include <boost/algorithm/string.hpp>
#include "log.hpp"
#include "cppjieba/Jieba.hpp"

namespace ns_util
{
    class FileUtil
    {
    public:
        static bool ReadFile(const std::string &file_path, std::string *out)
        {
            std::ifstream in(file_path, std::ios::in);
            if(!in.is_open())
            {
                LOG(ERROR,"read file error: %s", file_path.c_str());
                return false;
            }

            std::string line;
            while(std::getline(in, line))
            {
                *out += line;
            }

            in.close();
            return true;
        }
    };

    class StringUtil
    {
    public:
        static void Split(const std::string &target, std::vector<std::string> *out, const std::string &sep)
        {
            // 使用boost库中的字符串切分
            boost::split(*out, target, boost::is_any_of(sep), boost::token_compress_on);
        }
    };


    class JiebaUtil
    {
    private:
        // jieba工具需要使用的参数
        const char* const DICT_PATH = "./dict/jieba.dict.utf8";
        const char* const HMM_PATH  = "./dict/hmm_model.utf8";
        const char* const IDF_PATH  = "./dict/idf.utf8";
        const char* const USER_DICT_PATH = "./dict/user.dict.utf8";
        const char* const STOP_WORD_PATH = "./dict/stop_words.utf8";
    private:
        cppjieba::Jieba jieba;
        std::unordered_map<std::string, bool> stop_words; // 记录jieba工具中的暂停词，用于对分词之后的结果去掉暂停词
    private:
        JiebaUtil()
            :jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH)
        {}

        JiebaUtil(const JiebaUtil&) = delete;
        JiebaUtil& operator=(const JiebaUtil&) = delete;
        // 懒汉模式的单例
        static JiebaUtil* _instance;
    public:
        static void CutString(const std::string &src, std::vector<std::string> *out)
        {
            // 获取 JiebaUtil 单例
            ns_util::JiebaUtil::get_instance();
            // 初始化 JiebaUtil 单例
            _instance->InitJiebaUtil();
            // 进行分词
            _instance->CutStringHelper(src, out);
        }
    private:
        void CutStringHelper(const std::string &src, std::vector<std::string> *out)
        {
            // CutForSearch直接返回分词之后的结果
            // 但是分词之后的结果中会存在暂停词
            jieba.CutForSearch(src, *out);

            // 去掉暂停词
            for(auto iter = out->begin(); iter != out->end(); )
            {
                auto it = stop_words.find(*iter);
                if(it != stop_words.end())
                {
                    //说明当前的string 是暂停词，需要去掉
                    iter = out->erase(iter);
                }
                else
                {
                    iter++;
                }
            }
        }

        static JiebaUtil* get_instance()
        {
            static std::mutex mtx;
            if(nullptr == _instance)
            {
                mtx.lock();
                if(nullptr == _instance)
                {
                    _instance = new JiebaUtil();
                }
                mtx.unlock();
            }

            return _instance;
        }

        void InitJiebaUtil()
        {
            std::ifstream in(STOP_WORD_PATH);
            if(!in.is_open())
            {
                LOG(FATAL, "load stop words file error");
                return;
            }

            // 根据jieba内置的词典进行初始化JiebaUtil
            std::string line;
            while(std::getline(in, line))
            {
                stop_words.insert({line, true});
            }

            in.close();
        }
    };

    JiebaUtil *JiebaUtil::_instance = nullptr;
}